<?php

namespace App\Http\Controllers\Home\User;

use App\Http\Controllers\Controller;
use App\Models\Home\Client as ClientModel;
use App\Models\Home\ClientSchedule;
use Frame;
use Illuminate\Support\Facades\Auth;
use Tools;
use FileSystem;

class ClientScheduleController extends Controller
{
    protected $passField = [
        'client_id','assign','content','details','attachment','uid','result'
    ];
    protected $customer;

    public function __construct()
    {
        $this->customer = get_config_cache('customer');
    }

    public function add_schedule(ClientModel $obj){
        if(request() ->isMethod('post')){
            //验证
            $this->validate(request(), [
                'details_1' => 'required|string',
                'details_2' => 'required|string',
                'details_3' => 'required|string',
                'content' => 'nullable|string',
                'result' => 'integer',
                'shareTo' => 'required|integer',
            ],[],[
                'details_1'   => '佣金点',
                'details_2' => '年薪',
                'details_3'   => '回款时间',
                'content'   => '沟通记录',
                'result'   => '跟进结果',
                'shareTo' => '任务归属',
            ]);


            //数据库操作
            $data = [];

            $file_id = intval(request()->input('file_id'));
            if($file_id){
                $data['attachment'] = $file_id;
            }

            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField)){
                    $data[$key] = $val;
                }
            }
            $data['uid'] = Auth::id();
            $details_arr = [];
            $details_arr[] = request()->post('details_1')?clean(trim(request()->post('details_1'))):'';
            $details_arr[] = request()->post('details_2')?clean(trim(request()->post('details_2'))):'';
            $details_arr[] = request()->post('details_3')?clean(trim(request()->post('details_3'))):'';
            $data['details'] = implode('+',$details_arr);

            switch (intval(request()->post('shareTo'))){
                case 1:
                    $obj->assign = Auth::id();
                    $obj->assign_time = strtotime('+ '.$this->customer.' day',time());
                    $obj->share = 0;
                    break;
                case 2:
                    $obj->assign = '';
                    $obj->assign_time = '';
                    $obj->share = 1;
                    break;
                case 3:
                    if ($assign = intval(request()->post('assign'))){
                        $obj->assign = $assign;
                        $obj->assign_time = strtotime('+ '.$this->customer.' day',time());
                        $obj->share = 0;
                    }else{
                        $obj->assign = '';
                        $obj->assign_time = '';
                        $obj->share = 1;
                    }
                    break;
                default:
                    $obj->assign = '';
                    $obj->assign_time = '';
                    $obj->share = 1;
            }

            $obj->save();

            $data['assign'] = $obj->assign;
            $obj->all_schedule()->create($data);


            //返回结果
            return ['code'=>1];
        }

        $frame = Frame::GetFrame(config('base.branch.lg.id'),config('base.level.admin'));


        $obj->load(['user','assign_user.frame','all_schedule'=>function($query){
            $query->with(['user.frame.belong_user','assign_user']);
        }]);

        if (count($obj->all_schedule)){

            foreach ($obj->all_schedule as $k => $v){

                $obj->all_schedule[$k]->user_name = $v->user?$v->user->username:'';

                $obj->all_schedule[$k]->team_name = $v->user?$v->user->frame?$v->user->frame->belong_user?$v->user->frame->belong_user->username:'':'':'';

                $result = $v->result;

                switch ($result){
                    case 1:
                        $schedule_result = '待沟通';
                        break;
                    case 2:
                        $schedule_result = '有意向，跟进中';
                        break;
                    case 3:
                        $schedule_result = '未签合约';
                        break;
                    case 4:
                        $schedule_result = '已签合同';
                        break;
                    case 5:
                        $schedule_result = '不考虑';
                        break;
                    default:
                        $schedule_result = '待沟通';
                }

                $obj->all_schedule[$k]->schedule_result = $schedule_result;

            }

        }

        $obj = Tools::check_permission($obj);

        if ($obj->assign){
            if ($obj->assign == Auth::id()){
                $obj->shareTo = 1;
            }else{
                $obj->shareTo = 3;
            }
        }else{
            $obj->shareTo = 2;
        }

        $customer_day = $this->customer;

        return view('Home.User.Client.Common.add_client_schedule',compact('obj','frame','customer_day'));
    }

    public function download(ClientSchedule $obj){

        $fid = $obj->attachment;

        $res = FileSystem::downloadFile($fid,true);

        return response()->download($res['path'],$res['name']);

    }

}